package com.szholly.plug.arcgis.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.szholly.plug.arcgis.entity.GisLayerRow;
import com.szholly.plug.arcgis.entity.GisLayerTable;
import com.szholly.utils.freemarker.directive.DirectiveUtils;
import com.szholly.utils.freemarker.directive.IDirective;
import com.szholly.utils.spring.springmvc.route.FunctionInfo;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.QueryMap;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 定义获取所有配置的图层信息
 */
@Component("directive_layersObject")
public class LayersObjectDirective implements TemplateDirectiveModel, IDirective  {
	/**
	 * 分组名称
	 */
	private static String PARA_GROUPNAME = "groupName";
	
	@Override
	public FunctionInfo getFunctionInfo() {
		FunctionInfo info = new FunctionInfo("domains", 1, "表单",
				"定义获取所有配置的图层信息", null);
		return info;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void execute(Environment env, Map params, TemplateModel[] model,
			TemplateDirectiveBody body) throws TemplateException, IOException {

		String groupNamePara = DirectiveUtils.getString(PARA_GROUPNAME, params);
		
		QueryMap queryMap = new QueryMap();
		queryMap.getOrderByMap().put(GisLayerRow.FIELD_SGroupname, "1");
		queryMap.getOrderByMap().put(GisLayerRow.FIELD_IIndex, "0");
		if(!StringUtils.IsNullOrSpace(groupNamePara)){
			queryMap.getWhereMap().put(GisLayerRow.FIELD_SGroupname, groupNamePara);
		}
		queryMap.getWhereMap().put(GisLayerRow.FIELD_ISUSE, "是");
		List<GisLayerRow> layers = GisLayerTable.getSingleRef().
				getEntityList(GisLayerRow.class, queryMap);
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		
		if(layers!=null && layers.size()>0){
			for(GisLayerRow layer : layers){
				String layerName = layer.getSLayername();
				String layerAliasName = layer.getSName();
				String groupName = layer.getSGroupname();
				String type = layer.getSType();
				String url = layer.getSUrl();
				String visible = layer.getSVisible();
				String showids = layer.getSShowids();
				
				Map<String, String> map = new HashMap<String, String>();
				map.put("name", layerName);
				map.put("aliasName", layerAliasName);
				map.put("group", groupName);
				map.put("mtype", type);
				map.put("url", url);
				map.put("visible", visible);
				map.put("showids", showids);
				
				mapList.add(map);
			}
		}
		Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
				params);
		paramWrap.put("data", DEFAULT_WRAPPER.wrap(mapList));
		Map<String, TemplateModel> origMap = DirectiveUtils
				.addParamsToVariable(env, paramWrap);
		body.render(env.getOut());
		DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
	}
}
